﻿namespace TDMS.Net;

public abstract class TdmsObject : ITdmsObject
{
    protected TdmsPropertyCollection Properties = new();

    public IntPtr Handle { get; protected set; }

    public string Name { get; protected set; }

    public string Description { get; protected set; }

    public ITdmsContainerObject? Parent { get; protected set; }

    public virtual TdmsPropertyCollection GetProperties() => Properties;

    public virtual TdmsProperty CreateProperty(string property, ETdmsPropertyDataType dataType, object value)
    {
        ETdmsPropertyOwnerType ownerType;
        var implType = GetType();
        if (implType == typeof(TdmsFile))
        {
            ownerType = ETdmsPropertyOwnerType.File;
        }
        else if (implType == typeof(TdmsChannelGroup))
        {
            ownerType = ETdmsPropertyOwnerType.ChannelGroup;
        }
        else if (implType == typeof(TdmsChannel))
        {
            ownerType = ETdmsPropertyOwnerType.Channel;
        }
        else
        {
            throw new TdmsException("Not supported");
        }

        var tdmsProperty = new TdmsProperty(Handle, ownerType, property, dataType, value);
        Properties.Add(tdmsProperty);
        return tdmsProperty;
    }
}